package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bean.User;
import com.jms.fu.model.bo.AppBO;
import com.jms.fu.model.bo.AppDownBO;
import com.jms.fu.model.bo.TransactionBO;
import com.jms.fu.model.bo.UserBO;

public class BuyAppAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String role = (String) request.getSession().getAttribute("role");
			PrintWriter out = response.getWriter();
			String username = (String) request.getSession().getAttribute(
					"username");
			if (username == null) {
				out.print("login");
				return null;
			}

			int appid = Integer.parseInt(request.getParameter("appid"));
			AppBO appbo = new AppBO();
			AppDownBO appdown = new AppDownBO();
			App app = appbo.getAppByID(appid);
			UserBO userbo = new UserBO();
			User user = userbo.getUserInfoByUsername(username);
			float price = app.getPrice();
			if (price == 0 || appdown.checkAppOwnByUsername(username, appid)
					|| app.getDevId().equals(username)
					|| role.equals("Administrator")) {
				if (!appdown.checkAppOwnByUsername(username, appid)
						&& !app.getDevId().equals(username)
						&& !role.equals("Administrator")) {
					app.setNumberOfDownload(app.getNumberOfDownload() + 1);
					appbo.updateFullApp(app);
					appdown.addAppToUsername(username, appid,
							app.getUpVersion());
				}
				String appname = appbo.getNumberOfFile(
						getServlet().getServletContext().getRealPath("/")
								+ app.getLink()).get(0);
				out.print(app.getLink() + "/" + appname);
			} else {
				

				if (user.getBalance() < price) {
					System.out.println(price + " : " + user.getBalance());
					out.print("charge");
				} else {

					TransactionBO tbo = new TransactionBO();
					app.setNumberOfDownload(app.getNumberOfDownload() + 1);
					userbo.deposit(username, user.getBalance() - price);
					userbo.deposit(app.getDevId(), userbo
							.getUserInfoByUsername(app.getDevId()).getBalance()
							+ price);
					appbo.updateFullApp(app);
					appdown.addAppToUsername(username, appid,
							app.getUpVersion());
					tbo.addNewTransaction(username, appid, price, 0, 0);
					String appname = appbo.getNumberOfFile(
							getServlet().getServletContext().getRealPath("/")
									+ app.getLink()).get(0);
					out.print(app.getLink() + "/" + appname);
				}
			}

			out.close();
			return null;
		} catch (NumberFormatException | DAOException e) {
			e.printStackTrace();
			try {
				PrintWriter out = response.getWriter();
				out.print("failed");
				out.close();
			} catch (IOException e1) {
				return null;
			}
			return null;
		} catch (IOException e) {
			return null;
		}

	}

}
